<template>
    <view>
        <u-navbar :title="header" :background="background" back-icon-color="#efefef" title-color="#efefef">
            <view class="action">
                <view class="icon" @click="release()">
                    <u-icon label="发布" label-color="#efefef" label-size="32" size="40" name="/static/icon/fa.png">
                    </u-icon>
                </view>
            </view>
        </u-navbar>
        <view class="wrap">
            <view class="title" v-if="type==1">
                <view class="inTitle">
                    <u-input v-model="title" type="text" :custom-style="inputStyle" placeholder="请输入标题" />
                </view>
                <u-line color="black"></u-line>
            </view>
            <!-- <u-line color="black"></u-line> -->
            <unieditor :store_detail="store_detail" @getContents="getContents" class="bg" style="height: 70vh;">
            </unieditor>
            <!-- <button @click="test()">获取</button> -->
        </view>
        <u-toast ref="uToast" />
    </view>
</template>

<script>
    import unieditor from "@/components/editor/editor.vue"
    import { addForum, addComment } from "@/api/data.js"
    export default {
        components: {
            unieditor,
        },

        data() {
            return {
                background: {
                    backgroundImage: 'linear-gradient(45deg, rgb(255, 40, 0), rgb(147, 0, 0))'
                },
                store_detail: '',
                title: '',
                inputStyle: {
                    fontSize: '32rpx',
                },
                type: 1,
                header: '主题',
                forum: {
                    title: '',
                    content: '',
                    userName:'',
                    userId:'',
                },
                userInfo: {},
                forumId: 0,
                parentId:0,
                comment: {
                    userId:0,
                    content:'',
                    type: 0,
                    articleId:0,
                    parentId:0,
                }
                // username: '',
                // placeholder: '开始输入...',
                // content: `
                // 					<p>露从今夜白，月是故乡明</p>
                // 					<img src="https://cdn.uviewui.com/uview/swiper/2.jpg" />`,
                // mainstyle: {
                //     p: 'font-size:32rpx;',
                //     span: 'font-size: 30rpx',
                //     img: 'padding: 20rpx 0'
                // }
            }
        },
        onLoad(options) {
            this.type = options.type
            if(this.type==2){
                 this.forumId = options.forumId
                 this.parentId = options.articleId
            }
               
            // this.forum.username = options.user
            // console.log(this.username)
        },
        onShow() {
             this.userInfo = uni.getStorageSync('userInfo')
             
             
            if (this.type == 1){
                this.header = '主题'
                this.forum.userId = this.userInfo.id
                this.forum.userName = this.userInfo.username
            }
                
            if (this.type == 2){
                this.header = '回复'
                this.comment.userId = this.userInfo.id
                this.comment.articleId = this.forumId
                this.comment.parentId =this.parentId
            }
                
        },
        methods: {
            getContents(html) {
                this.store_detail = html
            },
            test() {
                console.log(this.store_detail)
            },
            release() {
                const _this=this
                if (this.store_detail == '') {
                    this.$refs.uToast.show({
                        title: '内容不能为空',
                        type: 'error',
                        position: 'top'
                        // url: '/pages/user/index'
                    })
                }
                if (this.type == 1) {
                    
                    if (this.title == '') {
                        this.$refs.uToast.show({
                            title: '标题不能为空',
                            type: 'error',
                            position: 'top'
                            // url: '/pages/user/index'
                        })
                        if (this.store_detail == '') {
                            this.$refs.uToast.show({
                                title: '标题内容不能为空',
                                type: 'error',
                                position: 'top'
                                // url: '/pages/user/index'
                            })
                        }
                    }
                    if(this.title!=''&&this.store_detail!=''){
                        this.forum.title = this.title
                        this.forum.content = this.store_detail
                        addForum(this.forum).then(function(res){
                            if(res.data.code==200){
                                uni.showLoading({
                                     title: '发布成功！'
                                })
                                setTimeout(function () {
                                    uni.hideLoading();
                                    uni.navigateBack({
                                        delta: 1
                                    });
                                }, 1500);
                            }
                            else{
                                _this.$refs.uToast.show({
                                    title: '出现错误！请重试！',
                                    type: 'error',
                                    position: 'top'
                                    // url: '/pages/user/index'
                                })
                            }
                        })
                    }
                    
                }
                if (this.type == 2) {
                    // this.forum.content = this.store_detail
                    if(this.store_detail!=''){
                        this.comment.content = this.store_detail
                        addComment(this.comment).then(function(res){
                            if(res.data.code==200){
                                uni.showLoading({
                                     title: '回复成功！'
                                })
                                setTimeout(function () {
                                    uni.hideLoading();
                                    uni.navigateBack({
                                        delta: 1
                                    });
                                }, 1500);
                            }
                            else{
                                _this.$refs.uToast.show({
                                    title: '出现错误！请重试！',
                                    type: 'error',
                                    position: 'top'
                                    // url: '/pages/user/index'
                                })
                            }
                        })
                    }
                    
                    // console.log(this.comment)
                    
                }
                // console.log(this.forum)
            }
        }
    }
</script>

<style lang="scss" scoped>
    .wrap {
        display: flex;
        flex-direction: column;
        height: calc(100vh - var(--window-top));
        width: 100%;
        background-color: #fffaf0;
        // background-color: #fff9ee;
    }

    .title {
        height: 80rpx;
    }

    .inTitle {
        padding: 5rpx 20rpx;
        font-size: 32rpx;
    }

    .action {
        width: 100%;

        .icon {
            float: right;
            padding-right: 30rpx;
        }
    }
</style>
